package com.ysd.lis.controller.bac;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.ysd.common.Result;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.bac.BacImageResult;
import com.ysd.lis.service.bac.BacImageResultService;
import com.ysd.lis.service.lab.LabRptresultService;
import com.ysd.util.PropertySlash;
import com.ysd.util.ToolsUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cfz
 * @since 2024-12-04
 */
@RestController
@RequestMapping("/api/bac/bac-image-result")
public class BacImageResultController extends BaseController {
    @Autowired
    BacImageResultService bacImageResultService;

    @Autowired
    LabRptresultService labRptresultService;

    @Value("${picturePath}")
    private String picturePath;

    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;

    @ApiOperation("查询")
    @GetMapping("/findBacImageResultList")
    public Result findBacImageResultList(BacImageResult bacImageResult) {
        return bacImageResultService.findBacImageResultList(bacImageResult);
    }

    @ApiOperation("增加")
    @PostMapping("/addBacImageResult")
    public Result addBacImageResult(@RequestBody BacImageResult bacImageResult) {
        return bacImageResultService.addBacImageResult(bacImageResult);
    }

    @ApiOperation("删除")
    @PostMapping("/deleteBacImageResult")
    public Result deleteBacImageResult(@RequestBody BacImageResult bacImageResult) {
        return bacImageResultService.deleteBacImageResult(bacImageResult);
    }

    @ApiOperation("修改")
    @PostMapping("/updateBacImageResult")
    public Result updateBacImageResult(@RequestBody BacImageResult bacImageResult) {
        return bacImageResultService.updateBacImageResult(bacImageResult);
    }

    @ApiOperation("上传检验图片")
    @PostMapping("/uploadImage")
    @LogDetail(operationType = OperationType.UPDATE, module = "图文报告上传检验图片")
    public Result uploadSignatureFile(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {

        String rptGroup = request.getParameter("rptGroup");
        String sampleda = request.getParameter("sampleda");
        String sampleNo = request.getParameter("sampleNo");
        String testFlowNo = request.getParameter("testFlowNo");
        String testNodeNo = request.getParameter("testNodeNo");
        String isolateNum = request.getParameter("isolateNum");
        String seq = request.getParameter("seq");
        String germNo = request.getParameter("germNo");
        String isSelected = request.getParameter("isSelected");
        if (StringUtils.isBlank(rptGroup) || StringUtils.isBlank(sampleda) || StringUtils.isBlank(sampleNo)) {
            return Result.fail("参数不能为空");
        }

        String id = request.getParameter("id");
        if (file == null) {
            return Result.fail("文件不能为空");
        }

        // 文件限制10M
        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("文件最大为 10M！");
        }
        String folder = picturePath + "\\" + sampleda.substring(0, 10) + "\\" + rptGroup + PropertySlash.getSlash();
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        Date nameDate = new Date();
        String imageName = sampleda.substring(0, 10) + rptGroup + sampleNo + rptGroup + Long.toString(nameDate.getTime()) ;
        String fileName = imageName + ".png";
        String path = folder + fileName;
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        if (file1.length() < 0) {
            return Result.fail("文件上传失败");
        }
        BacImageResult bacImageResult = new BacImageResult();
        bacImageResult.setId(id);
        //判断是否是获取相机数据,若是则直接指定图片名和扩展名
        if(file.getOriginalFilename().equals("blob")){
            bacImageResult.setImageName(imageName);
            bacImageResult.setImageExt("png");
        }else{
            bacImageResult.setImageName(file.getOriginalFilename());
            bacImageResult.setImageExt(FileNameUtil.getSuffix(file.getOriginalFilename()));
        }
        bacImageResult.setImageUrl("/image/" + sampleda.substring(0, 10) + "/" + rptGroup + "/" + fileName);

        bacImageResult.setRptGroup(rptGroup);
        bacImageResult.setSampleda(Timestamp.valueOf(sampleda.substring(0, 10) + " 00:00:00"));
        bacImageResult.setSampleNo(sampleNo);

        bacImageResult.setTestFlowNo(ToolsUtils.isEmpty(testFlowNo) ? null : testFlowNo);
        bacImageResult.setTestNodeNo(ToolsUtils.isEmpty(testNodeNo) ? null : testNodeNo);
        bacImageResult.setIsolateNum(ToolsUtils.isEmpty(isolateNum) ? null : isolateNum);
        bacImageResult.setSeq(ToolsUtils.isEmpty(seq) ? null : seq);
        bacImageResult.setGermNo(ToolsUtils.isEmpty(germNo) ? null : germNo);
        bacImageResult.setIsSelected(ToolsUtils.isEmpty(isSelected) ? null : Integer.parseInt(isSelected));

        this.labRptresultService.addMaininfo(rptGroup, sampleda, sampleNo);
        boolean r = bacImageResultService.saveOrUpdate(bacImageResult);

        return r ? Result.succ(1, "", bacImageResult) : Result.fail("截图失败");
    }
}
